package com.xy.lang.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @param <T>
 */
public class Page<T> implements Serializable {

	private static final long serialVersionUID = -1L;

	/**
	 *
	 */
	private List<T> rows;
	/**
	 *
	 */
	private int total;

	/**
	 *
	 */
	public Page() {

	}

	/**
	 *
	 *
	 * @param rows
	 * @param total
	 */
	public Page(List<T> rows, int total) {
		this.rows = rows;
		this.total = total;
	}

    /**
     *
     * @param originList
     * @param pageIndex
     * @param pageSize
     * @param <T>
     * @return
     */
	public static <T> Page<T> getPage(List<T> originList, Integer pageIndex, Integer pageSize) {
		pageIndex = pageIndex == null ? 0 : pageIndex;
		pageSize = pageSize == null ? 0 : pageSize;

		List<T> resultList = new ArrayList<>();

		if (pageIndex >= 0 && pageSize > 0) {
			int pageStart = pageIndex * pageSize;
			int pageStop = pageStart + pageSize;
			while (pageStart < pageStop) {
				if (pageStart == originList.size()) {
					break;
				}
				resultList.add(originList.get(pageStart++));
			}
		}
		return new Page<>(resultList,originList.size());
	}

	/**
	 *
	 * @param <T>
	 * @return
	 */
	public final static <T> Page<T> empty(){
		return new Page<>(Collections.emptyList(), 0);
	}

	public List<T> getRows() {
		return rows;
	}
	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}

}
